//-------------------------2523--------------------------
//-----------------作者Q: 549710689-------------------
//-----------------调试运行代码，完善代码, 定制代码-------------------
package com.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.GoodsDAO;
import com.dao.HistDAO;
import com.dao.UsersDAO;
import com.entity.Goods;
import com.entity.Hist;
import com.entity.Users;
import com.service.RecommendService;

@Service("recommendService")
public class RecommendServiceImpl implements RecommendService {
	@Autowired
	@Resource
	private HistDAO histDAO;
	// 注入DAO对象
	@Autowired
	@Resource
	private GoodsDAO goodsDAO;
	@Autowired
	@Resource
	private UsersDAO usersDAO;
	private String userid = "";
	// 相似用户集合
	private List<List<Object>> similarityUsers = null;
	// 推荐所有商品集合
	private List<String> targetRecommendGoods = null;
	// 浏览过商品集合
	private List<String> commentedGoods = null;
	// 用户在商品浏览集合中的位置
	private int targetUserIndex = 0;
	// 目标用户浏览过的商品
	private List<String> targetUserCommentedGoods = null;

	private String[] goods = null;

	@Override
	public List<Goods> getRecommend(String userid) {
		this.userid = userid;
		// 建立用户数组 除了当前用户外 随机抽取9个用户
		
		return null;
	}

	private void calcRecommendGoods(int[][] allUserGoodshist, int goodsNum) {
		
	}

	/**
	 * 把推荐列表中用户已经浏览过的商品剔除
	 */
	private void handleRecommendGoods(int[][] allUserGoodshist, int goodsNum) {
		int[] user2hist = new int[goodsNum];
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < goodsNum; j++) {
				user2hist[j] = allUserGoodshist[i][j];
			}
		}
		commentedGoods = new ArrayList<String>();
		for (int i = 0; i < user2hist.length; i++) {
			if (allUserGoodshist[0][i] != 0) {
				commentedGoods.add(goods[i]);
			}
		}
	}

	/**
	 * 获取两个最相似的用户
	 */
	private void calcUserSimilarity(int[][] allUserGoodshist, int goodsNum) {
		
	}

	/**
	 * 根据用户数据，计算用户相似度
	 * 
	 * @param user1hist
	 * @param user2hist
	 * @return
	 */
	private static double calcTwoUserSimilarity(int[] user1hist, int[] user2hist, int goodsNum) {
		double sum = 0;
		for (int i = 0; i < goodsNum; i++) {
			sum += Math.pow(user1hist[i] - user2hist[i], 2);
		}
		return Math.sqrt(sum);
	}

	/**
	 * 集合排序
	 */
	private static List<List<Object>> compare(List<List<Object>> tmpList) {
		for (int i = 0; i < tmpList.size(); i++) {
			for (int j = 0; j < tmpList.size() - i; j++) {
				List<Object> t1 = tmpList.get(i);
				List<Object> t2 = tmpList.get(j);
				if (Double.parseDouble("" + t1.get(1)) > Double.parseDouble("" + t2.get(1))) {
					List<Object> tmp = new ArrayList<Object>();
					tmp = t1;
					tmpList.set(i, t2);
					tmpList.set(j, tmp);
				}
			}
		}
		return tmpList;
	}

	public HistDAO getHistDAO() {
		return histDAO;
	}

	public void setHistDAO(HistDAO histDAO) {
		this.histDAO = histDAO;
	}

	public GoodsDAO getGoodsDAO() {
		return goodsDAO;
	}

	public void setGoodsDAO(GoodsDAO goodsDAO) {
		this.goodsDAO = goodsDAO;
	}

	public UsersDAO getUsersDAO() {
		return usersDAO;
	}

	public void setUsersDAO(UsersDAO usersDAO) {
		this.usersDAO = usersDAO;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public List<List<Object>> getSimilarityUsers() {
		return similarityUsers;
	}

	public void setSimilarityUsers(List<List<Object>> similarityUsers) {
		this.similarityUsers = similarityUsers;
	}

	public List<String> getTargetRecommendGoods() {
		return targetRecommendGoods;
	}

	public void setTargetRecommendGoods(List<String> targetRecommendGoods) {
		this.targetRecommendGoods = targetRecommendGoods;
	}

	public List<String> getCommentedGoods() {
		return commentedGoods;
	}

	public void setCommentedGoods(List<String> commentedGoods) {
		this.commentedGoods = commentedGoods;
	}

	public int getTargetUserIndex() {
		return targetUserIndex;
	}

	public void setTargetUserIndex(int targetUserIndex) {
		this.targetUserIndex = targetUserIndex;
	}

	public List<String> getTargetUserCommentedGoods() {
		return targetUserCommentedGoods;
	}

	public void setTargetUserCommentedGoods(List<String> targetUserCommentedGoods) {
		this.targetUserCommentedGoods = targetUserCommentedGoods;
	}

	public String[] getGoods() {
		return goods;
	}

	public void setGoods(String[] goods) {
		this.goods = goods;
	}

}
